<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('库区管理')"/>

<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
            <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                    <div class="layui-inline">
                        <label class="layui-form-label">库区名称</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="mc" name="mc" placeholder="库区名称"/>
                        </div>
                    </div>
                    <!--<div class="layui-inline">
                        <label class="layui-form-label">库区代码</label>
                        <input type="text" class="layui-input" id="dm" name="dm" placeholder="库区代码"/>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">激活状态</label>
                        <select name="jhzt" lay-filter="aihao">
                            <option value=""></option>
                            <option value="0">全部</option>
                            <option value="1">已激活</option>
                            <option value="2">未激活</option>
                        </select>
                    </div>-->

                    <div class="layui-inline inline-search-btn">
                        <a class="layui-btn layui-btn-normal" data-type="reload">查询</a>
                    </div>

                    <div class="layui-inline inline-reset-btn">
                        <button type="reset" class="layui-btn layui-btn-normal" >重置</button>
                    </div>
                </form>
                <!--<div class="moreBtn"><i class="layui-icon layui-icon-triangle-d" style="color: #1E9FFF;"></i></div>-->
                <div class="group-button tpl-button" id="toolbar">
                </div>
                <!--列表-->
                <section class="panel panel-padding layui-card-body">
                    <div class="pro">
                        <table id="table"></table>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>

</body>


<div th:include="public/public_js::public_js"></div>
<div th:include="public/public_btn::public_btn"></div>
<style type="text/css">
    .moreBtn {
        width: 50px;
        height: 20px;
        text-align: center;
        color: #fff;
        line-height: 20px;
        background-color: #eee;
        position: relative;
        margin: 0 auto;
        cursor: pointer;
    }

    .moreBtn:before {
        content: '';
        display: block;
        width: 35px;
        height: 20px;
        position: absolute;
        transform: skewX(30deg);
        background: #eee;
        border-top-left-radius: 8px;
        left: -20px;
        top: 0;
    }

    .moreBtn:after {
        content: '';
        display: block;
        width: 35px;
        height: 20px;
        position: absolute;
        transform: skewX(-30deg);
        background: #eee;
        border-top-right-radius: 8px;
        top: 0;
        right: -20px;
    }
</style>
<script th:inline="javascript">
    var functions = [[${functions}]];
    layui.use(['list', 'layer', 'zzpUtils', 'jquery', 'laydate', 'tablebutton'], function () {
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate,
            tablebtn = layui.tablebutton;
        var $table;
        tablebtn.buildOnClick({
            type: 1,
            temp: $("#function_tpl"),
            functions: functions.headBtn,
            row: null,
            event: {
                addEvent: function (data) {
                    console.log("库区")
                    console.log(data);
                },
                /*编辑*/
                editEvent: function (data) {
                    if (data.row != undefined) {
                        editDialog(data.row.id);
                    }
                },


                exportEvent: function(data){
                    var arr=[];
                    var $rows = $table.bootstrapTable('getSelections');
                    if ($rows.length >0) {
                        arr = $.map($rows, function (row) {
                            return row.id;
                        })
                    }
                    /*
                     * 以下是将ids和对象拼接在一起
                     * */
                    var str=zzpUtils.getFormJson($("#searchForm"));
                    var object2 = Object.assign({ids: arr}, str);
                    /*将条件查询和ids传到后台*/
                    zzpUtils.outputXslx({
                        url:"/wmslibraryarea/outPutKqExcel",
                        data:JSON.stringify(object2)
                    });
                }
            }
        });
        //编辑库区
        function editDialog(id) {
            /*if (jhzt == 2) {
                layer.msg("该库区已禁用，不可编辑！");
            } else {*/
                zzpUtils.dialog({
                    title: "修改库区",
                    type: 2,
                    content: "/kuqu/editKq?id=" + id,
                    area: ['60%', '65%'], //设置dialog大小，也可以不设置，
                    end: function () {//修改成功后刷新
                        zzpUtils.refreshTable("#table");
                    }
                });
           /* }*/
        }
/*
        //禁用库区
        function stopDialog(id) {
            var ids = [];
            ids.push(id);
            zzpUtils.confirm("是否确定禁用库区", function (index) {
                zzpUtils.action({
                    url: "/wmslibraryarea/stopLibraryArea",
                    data: JSON.stringify({ids: ids}),
                    success: function (data) {
                        setTimeout(function () {
                            layer.msg(data.title);
                        }, 500);
                        zzpUtils.refreshTable("#table");
                    }, error: function (xhr, textStatus) {
                        console.log(textStatus);
                        console.log(xhr)
                    }
                })
            })
            return false;

        }

        //启用库区
        function startDialog(id) {
            var ids = [];
            ids.push(id);
            zzpUtils.confirm("是否确定启用库区", function (index) {
                zzpUtils.action({
                    url: "/wmslibraryarea/startLibraryArea",
                    data: JSON.stringify({ids: ids}),
                    success: function (data) {
                        setTimeout(function () {
                            layer.msg(data.title);
                        }, 500);
                        zzpUtils.refreshTable("#table");
                    }, error: function (xhr, textStatus) {
                        console.log(textStatus);
                        console.log(xhr)
                    }
                })
            })
            return false;
        }*/

        laydate.render({
            elem: '#test5'
            , theme: '#20a0ff'
        })
        $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            isOperate: true,
            functions: functions.tableBtn,
            url: '/wmslibraryarea/list',
            columns: [

                {
                field: 'ck_mc',
                title: '仓库'
            }, {
                field: 'mc',
                title: '库区名称'
            }, {
                field: 'dm',
                title: '库区代码'
            }
            /*, {
                field: 'num',
                title: '拥有库位数',
                formatter: function (value, row, index) {
                    alert(value+"=="+index+"--"+row.num);
                    var url = '<a href="#"  style="color:blue"  onclick="cha_kuwei(\'' + row.id + '\')">' + value + '</a>  ';
                    return url;
                }
            }, {
                field: 'jhzt',
                title: '激活状态',
                formatter: function (value, row, index) {
                    if (row.jhzt == 2) {
                        return [
                            '<a class="layui-btn layui-btn-primary layui-btn-xs" >禁用</a>'
                        ].join('')
                    } else {
                        return [
                            '<a class="layui-btn layui-btn-primary layui-btn-xs" >启用</a>'
                        ].join('')
                    }
                }
            }*/
            , {
                field: 'bz',
                title: '备注'
            }

            ],
            queryParams: function () {//查询条件
                return zzpUtils.getFormJson($("#searchForm"));
            },
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            },
            /*定义表格右侧按钮*/
            operateFormatter: operateFormatter
       });

        function operateFormatter(value, row, index) {
            var btn = '', button = '', buttons1 = '', buttons2 = '';
            for (var i = 0; i < functions.tableBtn.length; i++) {
                var item = functions.tableBtn[i];
                //更多之外
                buttons1 += '<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="' + item.event + '">'
                    + item.name
                    + '</button>';
            }

            return buttons1;
        }

        //查询
        $(".inline-search-btn").on('click', function () {
            zzpUtils.refreshTable("#table");
        })
        /*新增库区*/
        $(".add-btn").on('click', function () {
            zzpUtils.dialog({
                title: "添加库区",
                type: 2,
                content: "/kuqu/addKq",
                area: ['55%', '55%'], //设置dialog大小，也可以不设置，
                end: function () {//增加成功后刷新
                    zzpUtils.refreshTable("#table");
                }
            });
        })
        /*顶部按钮编辑库区*/
        $(".edit-btn").on('click', function () {
            var $rows = $table.bootstrapTable('getSelections');
            if ($rows.length == 1) {
                var ids = $.map($rows, function (row) {
                    return row.id;
                })
                /*if($rows[0].jhzt==2){
                    layer.msg("该库区已禁用，不可编辑！");
                }else {*/
                    zzpUtils.dialog({
                        title: "修改库区",
                        type: 2,
                        content: "/kuqu/editKq?id=" + ids,
                        area: ['55%', '55%'], //设置dialog大小，也可以不设置，
                        end: function () {//修改成功后刷新
                            zzpUtils.refreshTable("#table");
                        }
                    });
                /*}*/
            } else {
                layer.msg("请选择一条进行修改");
            }
        })
        /*顶部按钮多选禁用*/
        $("#btn_jin").on('click', function () {
            var index = 0;
            var $rows = $table.bootstrapTable('getSelections');
            //判断选中的库区里是否有已经禁用的
            for (var i = 0; i < $rows.length; i++) {
                if ($rows[i].jhzt == 2) {
                    index++;
                }
            }
            if (index > 0) {
                layer.msg("您选择的库区有已经禁用的，请重选！");
            } else if ($rows.length == 0) {
                layer.msg("请选择一条进行禁用");
            } else {
                var ids = $.map($rows, function (row) {
                    return row.id;
                })
                zzpUtils.confirm("是否确定禁用库区", function (index) {
                    zzpUtils.action({
                        url: "/wmslibraryarea/stopLibraryArea",
                        data: JSON.stringify({ids:ids}),
                        success: function (data) {
                            setTimeout(function () {
                                layer.msg(data.title);
                            }, 500);
                            zzpUtils.refreshTable("#table");
                        }, error: function (xhr, textStatus) {
                            console.log(textStatus);
                            console.log(xhr)
                        }
                    })
                })
                return false;

            }

        })
        /*顶部按钮导出 */
        $(".out-put").on('click', function () {
            var arr=[];
            var $rows = $table.bootstrapTable('getSelections');
            if ($rows.length >0) {
                arr = $.map($rows, function (row) {
                    return row.id;
                })
            }
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "http://localhost:8080/zxsswms/wmslibraryarea/outPutKqExcel", true);
            xhr.responseType = "blob";
            xhr.setRequestHeader('Accept', 'application/json');
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.setRequestHeader('Charset', 'utf-8');
            xhr.onload = function() {
                if (this.status == 200) {
                    var name = xhr.getResponseHeader("Content-disposition");
                    name=decodeURI(name);
                    var filename = name.substring(20,name.length);
                    var blob = new Blob([xhr.response], {type: 'text/xls'});
                    var csvUrl = URL.createObjectURL(blob);
                    var link = document.createElement('a');
                    link.href = csvUrl;
                    link.download = filename;
                    link.click();
                }
            };
            /*
            * 以下是将ids和对象拼接在一起
            * */
            var str=zzpUtils.getFormJson($("#searchForm"));
            var object2 = Object.assign({ids: arr}, str);
            /*将条件查询和ids传到后台*/
            xhr.send(JSON.stringify(
                object2
            ));
        })//导出结束

        /*顶部按钮多选启用*/
        $("#btn_qi").on('click', function () {
            var index = 0;
            var $rows = $table.bootstrapTable('getSelections');
            //判断选中的库区里是否有已经启用的
            for (var i = 0; i < $rows.length; i++) {
                if ($rows[i].jhzt == 1) {
                    index++;
                }
            }
            if (index > 0) {
                layer.msg("您选择的库区有已启用的，请重选！");
            } else if ($rows.length == 0) {
                layer.msg("请选择一条信息进行启用");
            } else {
                var ids = $.map($rows, function (row) {
                    return row.id;
                })
                zzpUtils.confirm("是否确定启用库区", function (index) {
                    zzpUtils.action({
                        url: "/wmslibraryarea/startLibraryArea",
                        data: JSON.stringify({ids:ids}),
                        success: function (data) {
                            setTimeout(function () {
                                layer.msg(data.title);
                            }, 500);
                            zzpUtils.refreshTable("#table");
                        }, error: function (xhr, textStatus) {
                            console.log(textStatus);
                            console.log(xhr)
                        }
                    })
                })
                return false;
            }
        })
    });

    /*查看库位*/
    function cha_kuwei(id) {
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;
        zzpUtils.dialog({
            title: "查看库位",
            type: 2,
            content: "/kuqu/search_kw?id=" + id,
            area: ['60%', '60%'], //设置dialog大小，也可以不设置，
        });
    }
    /*双击查看详情*/
    function btn_cha(id){
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;
        zzpUtils.dialog({
            title: "库区详情",
            type: 2,
            content: "/kuqu/getFindKq?id=" + id,
            area: ['50%', '50%'], //设置dialog大小，也可以不设置，
        });

    }
</script>
</html>